package com.twu.biblioteca.controller;

import com.twu.biblioteca.io.InputReader;
import com.twu.biblioteca.io.InputValidator;
import com.twu.biblioteca.model.Library;
import com.twu.biblioteca.processor.*;
import com.twu.biblioteca.ui.*;
import com.twu.biblioteca.util.Messages;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by madhu on 27/4/14.
 */
public class LibraryController {

    private LibraryView libraryView;
    private Library library;

    public LibraryController() {
        libraryView = LibraryView.getInstance();
        openLibrary();
    }

    private void openLibrary() {
        library = new Library();
        libraryView.displayWelcomeMessage(Messages.welcomeMessage);
    }

    public Library getLibrary() {
        return library;
    }

    public LibraryView getLibraryView() { return libraryView; }

    public void accessLibrary() {

        libraryView.createMenu();
        Map<Integer, MenuProcessor> menuProcessors = loadMenuProcessors();
        while(true) {
            libraryView.displayMenu();
            int menuOption = getMenuOption();
            manageItemsByMenuOption(menuOption, menuProcessors);
        }

    }

    private Map<Integer, MenuProcessor> loadMenuProcessors() {
        Map<Integer, MenuProcessor> processors = new HashMap<Integer, MenuProcessor>();
        processors.put(1, new ListBooksMenuProcessor());
        processors.put(2, new ListMoviesMenuProcessor());
        processors.put(3, new LoginMenuProcessor());
        processors.put(4, new CheckoutBookMenuProcessor());
        processors.put(5, new ReturnBookMenuProcessor());
        processors.put(6, new CheckoutMovieMenuProcessor());
        processors.put(7, new UserProfileMenuProcessor());
        processors.put(8, new QuitMenuProcessor());
        return processors;
    }

    private int getMenuOption()  {

        InputReader inputReader = new InputReader();
        int menuOption;
        boolean isValidMenuOption;

        do {

            while(true) {
                try {
                    menuOption = Integer.parseInt(inputReader.getUserInput());
                }
                catch(NumberFormatException nfe) {
                    libraryView.displayErrorMessage(Messages.notANumberErrorMessage);
                    continue;
                }
                break;
            }

            isValidMenuOption = validateMenuOption(menuOption, 1, libraryView.getMenu().size());
            if(!isValidMenuOption) libraryView.displayErrorMessage(Messages.invalidMenuOptionMessage);

        }while(!isValidMenuOption);

        return menuOption;

    }

    public boolean validateMenuOption(int menuOption, int startMenuIndex, int endMenuIndex) {

        if(menuOption>=startMenuIndex && menuOption<=endMenuIndex) return true;
        else return false;

    }

    public void manageItemsByMenuOption(int menuOption, Map<Integer, MenuProcessor> processors) {
        MenuProcessor menuProcessor = processors.get(menuOption);
        menuProcessor.processLibraryMenu(this);
    }

}
